package com.ruoyi.video.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.Apps.Utils.AppUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.video.mapper.VideoImagesMapper;
import com.ruoyi.video.domain.VideoImages;
import com.ruoyi.video.service.IVideoImagesService;

/**
 * videoService业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-23
 */
@Service
public class VideoImagesServiceImpl implements IVideoImagesService 
{
    @Autowired
    private VideoImagesMapper videoImagesMapper;

    /**
     * 查询video
     * 
     * @param id video主键
     * @return video
     */
    @Override
    public VideoImages selectVideoImagesById(Long id)
    {
        return videoImagesMapper.selectVideoImagesById(id);
    }

    /**
     * 查询video列表
     * 
     * @param videoImages video
     * @return video
     */
    @Override
    public List<VideoImages> selectVideoImagesList(VideoImages videoImages)
    {
        return videoImagesMapper.selectVideoImagesList(videoImages);
    }

    /**
     * 新增video
     * 
     * @param videoImages video
     * @return 结果
     */
    @Override
    public int insertVideoImages(VideoImages videoImages)
    {
        return videoImagesMapper.insertVideoImages(videoImages);
    }

    /**
     * 修改video
     * 
     * @param videoImages video
     * @return 结果
     */
    @Override
    public int updateVideoImages(VideoImages videoImages)
    {
        return videoImagesMapper.updateVideoImages(videoImages);
    }
    @Override
    public int resetImage(Long videoId, List<JSONObject> list) {
        videoImagesMapper.deleteVideoImagesByVideoId(videoId);
        if (StringUtils.isNotNull(list) && !list.isEmpty()) {
            List<VideoImages> videoImages = new ArrayList<>();
            for(JSONObject object: list) {
                VideoImages images = new VideoImages();
                images.setPath( AppUtils.filterFilePath(object.getString("path")));
                images.setTime(object.getLong("time"));
                videoImages.add( images );
            }
            videoImagesMapper.insertVideoImageList(videoId, videoImages);
        }
        return list.size();
    }

    /**
     * 批量删除video
     * 
     * @param ids 需要删除的video主键
     * @return 结果
     */
    @Override
    public int deleteVideoImagesByIds(Long[] ids)
    {
        return videoImagesMapper.deleteVideoImagesByIds(ids);
    }

    /**
     * 删除video信息
     * 
     * @param id video主键
     * @return 结果
     */
    @Override
    public int deleteVideoImagesById(Long id)
    {
        return videoImagesMapper.deleteVideoImagesById(id);
    }

    public int deleteVideoImagesByVideoId(Long videoId) {
        VideoImages search = new VideoImages();
        search.setVideoId( videoId );
        List<VideoImages> list = selectVideoImagesList( search );
        if (list.size() > 0) {
            for (VideoImages videoImages: list) {
                FileUtils.deleteFile( videoImages.getPath() );
                deleteVideoImagesById( videoImages.getId() );
            }
        }
        return list.size();
    }
}
